package com.uniledger.exchange.model;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class EncryptInterceptor implements Interceptor{

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (request.method().equalsIgnoreCase("post")){
            RequestBody requestBody = request.body();

            FormBody formBody = (FormBody) requestBody;
            int size = formBody.size();

            TreeMap<String, String> params = new TreeMap<>();
            for (int i = 0; i < size; i++) {
                String name = formBody.encodedName(i);
                String value = formBody.encodedValue(i);
                params.put(name, value);
            }

            //TODO 签名
            params.put("stellar_trx_sign", "sign");

            // convert params to new formBody
            FormBody.Builder builder = new FormBody.Builder();

            Set<Map.Entry<String, String>> newEnterySet = params.entrySet();
            for (Map.Entry<String, String> entry : newEnterySet) {
                builder.add(entry.getKey(), entry.getValue());
            }

        }

        return chain.proceed(request);
    }

}
